package jmine.tec.scheduling.services.incluir;

import static jmine.tec.services.export.impl.ExportUtils.put;
import static jmine.tec.services.export.impl.ExportUtils.putDatabaseId;
import static jmine.tec.services.export.impl.ExportUtils.setService;
import jmine.tec.database.pack.PackException;
import jmine.tec.database.pack.services.PackEntityPersistenceHelper;
import jmine.tec.di.annotation.Injected;
import jmine.tec.scheduling.domain.ScheduledTask;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.Output;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.RawInput;
import jmine.tec.services.api.export.ReferenceMap;
import jmine.tec.services.api.export.ServiceFiller;
import jmine.tec.services.io.ServiceBean;

/**
 * Includes a new scheduled task
 * 
 * @author ricardo.abe
 */
@ServiceImplementor(action = ActionsEnum.INCLUIR)
public class ScheduledTaskService implements ServiceFiller<ScheduledTask> {
    public static final String DATABASE_ID = "Database ID";

    public static final String REF_ID = "ID";

    public static final String MNEMONIC = "Mnemônico";

    public static final String DESCRIPTION = "Descrição";

    public static final String ACTIVE = "Ativo";

    public static final String CRON = "Cron expression";

    public static final String CONDITION = "Condição";

    public static final String ACTION = "Ação";

    private PackEntityPersistenceHelper helper;

    private RawInput rawInput;

    private Long id;

    private String mnemonic;

    private String description;

    private Boolean active;

    private String cron;

    /**
     * Includes a new task
     * 
     * @return new task
     * @throws PackException PackException
     */
    @Execution
    @Output(propertyName = REF_ID)
    public ScheduledTask execute() throws PackException {
        ScheduledTask task = this.helper.findOrCreate(ScheduledTask.class, this.id, this.mnemonic);
        task.setMnemonic(this.mnemonic);
        task.setDescription(this.description);
        task.setActive(this.active);
        task.setAction(this.rawInput.getRaw(ACTION));
        task.setCron(this.cron);
        task.setCondition(this.rawInput.getRaw(CONDITION));
        this.helper.save(task, this.id);
        return task;
    }

    /**
     * {@inheritDoc}
     */
    public void fillServiceBean(ServiceBean bean, ReferenceMap referenceMap, ScheduledTask entity) {
        setService(bean, this.getClass());
        putDatabaseId(bean, DATABASE_ID, entity);
        put(bean, MNEMONIC, entity.getMnemonic());
        put(bean, DESCRIPTION, entity.getDescription());
        put(bean, ACTIVE, entity.getActive());
        put(bean, ACTION, entity.getAction());
        put(bean, CRON, entity.getCron());
        put(bean, CONDITION, entity.getCondition());
    }

    /**
     * @param helper the helper to set
     */
    @Injected
    public void setHelper(PackEntityPersistenceHelper helper) {
        this.helper = helper;
    }

    /**
     * @param id the id to set
     */
    @Input(fieldName = DATABASE_ID, required = false)
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param mnemonic the mnemonic to set
     */
    @Input(fieldName = MNEMONIC)
    public void setMnemonic(String mnemonic) {
        this.mnemonic = mnemonic;
    }

    /**
     * @param description the description to set
     */
    @Input(fieldName = DESCRIPTION)
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @param active the active to set
     */
    @Input(fieldName = ACTIVE)
    public void setActive(Boolean active) {
        this.active = active;
    }

    /**
     * @param cron the cron to set
     */
    @Input(fieldName = CRON)
    public void setCron(String cron) {
        this.cron = cron;
    }

    /**
     * @param rawInput raw input.
     */
    @Injected
    public void setRawInput(RawInput rawInput) {
        this.rawInput = rawInput;
    }
}
